﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Helper;
using SchoolManagementSystem.Controls;
using SchoolManagementSystem.Objects;
namespace SchoolManagementSystem.Forms
{
    public partial class frmConfig: DevExpress.XtraEditors.XtraForm
    {
        public frmConfig()
        {
            InitializeComponent();
        }
        string con = @"Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}";
        string DB
        {
            get { return txtDB.Text; }
            set { }
        }
        string Server
        {
            get { return txtserver.Text; }
            set { }
        }
        string User
        {
            get { return txtuser.Text; }
            set { }
        }
        string Pass
        {
            get { return txtpassword.Text; }
            set { }
        }
        private void cmdtest_Click(object sender, EventArgs e)
        {
            if (IsRequired())
                return;
            if (RegisterService.RegisterServices.DBService.TestConnect(String.Format(con, Server, DB, User, Pass)))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Kết nối thành công");
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Kết nối không thành công");
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (IsRequired())
                return;
            try
            {
                FileHelper.SaveConnectString(String.Format(con, Server, DB, User, Pass));
                DevExpress.XtraEditors.XtraMessageBox.Show("Cấu hình đã được lưu, vui lòng restart lại ứng dụng");
            }catch
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Không thể lưu cấu hình, vui lòng thử lại !");
            }
        }

        void LoadData()
        {
            try
            {
                string cn = FileHelper.ConnectionString;
                if (cn != null)
                {
                    var data = cn.Split(';');
                    txtserver.Text = data[0].Replace("Data Source=", "");
                    txtDB.Text = data[1].Replace("Initial Catalog=", "");
                    txtuser.Text = data[3].Replace("User ID=", "");
                    txtpassword.Text = data[4].Replace("Password=", "");
                }
            }
            catch
            {
               
            }
        }

        private void frmConfig_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        bool IsRequired()
        {
            bool result = false;
            if(String.IsNullOrEmpty(txtserver.Text))
            {
                result = true;
                ValidateControls.Required(lblrequired1);
            }
            else
            {
                result = false;
                ValidateControls.NoneRequired(lblrequired1);
            }
            if (String.IsNullOrEmpty(txtDB.Text))
            {
                result = true;
                ValidateControls.Required(lblrequired2);
            }
            else
            {
                result = false;
                ValidateControls.NoneRequired(lblrequired2);
            }

            if (String.IsNullOrEmpty(txtuser.Text))
            {
                result = true;
                ValidateControls.Required(lblrequired3);
            }
            else
            {
                result = false;
                ValidateControls.NoneRequired(lblrequired3);
            }

            if (String.IsNullOrEmpty(txtpassword.Text))
            {
                result = true;
                ValidateControls.Required(lblrequired4);
            }
            else
            {
                result = false;
                ValidateControls.NoneRequired(lblrequired4);
            }
            return result;
        }
       
    }
}
